class Node{
    constructor(element){
        this.element = element;
        this.prev = null;
        this.next = null
    }
}
class DoubleList{
    constructor(){
        this.head = null;
        this.tail = null;
        this.count = 0;
    }
    add(element){//添加⼀个新元素。
        let node = new Node(element)
        let current;//指针
        if(this.head===null){//判断链表是否为空
            this.head = node
            this.tail = node;
        }else{
            current = this.head;
            while(current.next != null){
                current = current.next//指针改变
            }
            current.next = node
        }
        this.count++
    }
    indexOf(element){//根据元素查找下标
        let current = this.head;
        for(let i=0;i<this.count&&current!=null;i++){
            if(element === current.element){
                return i
            }
            current = current.next
        }
        return -1
    }
    getElementAt(index){//查找指定元素
        if(index>=0&&index<this.count){
            let current = this.head;
            for(let i=0;i<index;i++){
                current = current.next
            }
            return current
        }
        return undefined
    }
    insert(index,element){//向链表的特定位置插⼊⼀个节点
        if(index>=0&&index<this.count){
            //插入成功
            let node = new Node(element); //新生节点
            let current = this.head;
            if(index===0){//往头部插入
                if(this.head===null){
                    this.head = node;
                    this.tail = node
                }else{
                    node.next = this.head;
                    current.prev = node;
                    this.head = node
                }
            }else if(index === this.count){//往尾部添加
                current = this.tail;
                current.next = node;
                node.prev = current;
                this.tail = node;
            }else{
                const previous = this.getElementAt(index-1)
                current = this.tail
                node.next = current;
                previous.next = node;
                // current.prev = node;
                node.prev = previous
            }
            this.count++
            return true
        }
        //失败
        return false
    }
    // find(item){
    //     let current = this.head;
    //     while(current.element!=item){
    //         current = current.next
    //     }
    //     return current;
    // }
    //删除元素
    remove(element){
        let index = this.indexOf(element);
        return this.removeAt(index)
    }
    //删除元素
    removeAt(index){
        if(index>=0&&index<this.count){
            let current = this.head;
            if(index===0){//删除第一个节点
                this.head = current.next
            }else{
                let previous = this.getElementAt(index-1)//获取上一个节点
                current = previous.next;
                previous.next = current.next
            }
            this.count--;
            return current.element
        }
        return undefined
    }
    // print(){//打印
    //     if(this.head===null){
    //         return null
    //     }
    //     let current = this.head;
    //     for(let i=0;i<this.count && current!=null;i++){
    //     }
    // }
    print(){//打印
        let current = this.head;
        if(current){
            while(current.next != null){
                console.log(current);
                current = current.next;
            }
            console.log(current)
        }
    }
    clear(){//清空链表
        this.head = null;
        this.tail = null;
        this.count = 0;
    }
}

let list1 = new DoubleList();
    list1.add(3)
    list1.add(5)
    list1.insert(3,4)
    list1.insert(4,8)
    list1.print();
//     // Item{_name:"3",_value:3,_prev:Item,_next:Item}
//     // Item{_name:"4",_value:4,_prev:Item,_next:Item}
//     // Item{_name:"8",_value:8,_prev:Item,_next:Item}
//     // Item{_name:"5",_value:5,_prev:Item,_next:Item}
//     list1.remove(4);
//     list1.print()
//     // Item{_name:"3",_value:3,_prev:Item,_next:Item}
//     // Item{_name:"8",_value:8,_prev:Item,_next:Item}
//     // Item{_name:"5",_value:5,_prev:Item,_next:Item}
//     console.log(list.find(4));//null
//     console.log(list1.reverse_find(3))//3
//     list1.clear();